package paolo.android.worldclock;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TimeZone;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class AddTimeZone extends Activity {
	String tzids[] = null;
	List<TimeZoneEntry> timezones = new ArrayList<TimeZoneEntry>();
	// backup copy
	List<TimeZoneEntry> timezonesorig = new ArrayList<TimeZoneEntry>(); 
	ArrayAdapter<TimeZoneEntry> adapter = null;
	
	ListView tzlist = null;
	EditText tzfilter = null;
	
	TimeZoneEntry current = null;
	SQLiteDatabase db = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.time_zone_list);
        
        db = (new SQLiteHelper(this)).getWritableDatabase();
        current = new TimeZoneEntry();
        
        tzlist = (ListView)findViewById(R.id.tzlist);
        //tzlist.setTextFilterEnabled(true);
        tzlist.setOnItemClickListener(onListClick);
        adapter = new ArrayAdapter<TimeZoneEntry>(this, android.R.layout.simple_list_item_1, timezones);
        tzlist.setAdapter(adapter);
        
        timezonesorig = new ArrayList<TimeZoneEntry>();
		tzids = TimeZone.getAvailableIDs();
		for (int i = 0; i < tzids.length; i++) {
			TimeZoneEntry tze = new TimeZoneEntry(tzids[i]);
			timezonesorig.add(tze);
		}        
        
        tzfilter = (EditText)findViewById(R.id.tzsearch);
        tzfilter.addTextChangedListener(new FilterWatcher());        
        
        populateListOfAllTZs();
        
        
    }
    
	@Override
	public void onDestroy() {
		super.onDestroy();
		
		db.close();
	}    
    
    private void populateListOfAllTZs() {
    	adapter.clear();
    	
    	for (int i = 0; i < tzids.length; i++) {
    		TimeZoneEntry tze = new TimeZoneEntry(tzids[i]);
    		adapter.add(tze);
    	}
    	
    }
    
	private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			current = timezones.get(position);
			
			if (current.alreadySaved(db)) {
				Toast.makeText(AddTimeZone.this, current.getId() + " already added", Toast.LENGTH_SHORT).show();			
			}
			else {
				current.save(db);
				Toast.makeText(AddTimeZone.this, "Added " + current.getId(), Toast.LENGTH_SHORT).show();
			}
		}
	};
	
	class FilterWatcher implements TextWatcher {
		public void afterTextChanged(Editable s) {
			
		}
		
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			
		}
		
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			adapter.clear();
									
			Iterator<TimeZoneEntry> tzi = timezonesorig.iterator();
			while(tzi.hasNext()) {
				TimeZoneEntry tze = tzi.next();
				if (tze.toString().toUpperCase().contains(s.toString().toUpperCase())) {
					adapter.add(tze);
				}
			}
			
		}
	}
    
}